bitkeeper revision 1.1236.1.29 (421facf0U_Bl4zERFH7SO_Vg0H8Ebg)
authorakw27@arcadians.cl.cam.ac.uk <akw27@arcadians.cl.cam.ac.uk>
Fri, 25 Feb 2005 22:55:44 +0000 (22:55 +0000)
committerakw27@arcadians.cl.cam.ac.uk <akw27@arcadians.cl.cam.ac.uk>
Fri, 25 Feb 2005 22:55:44 +0000 (22:55 +0000)
Move xcs to unix domain sockets.

signed-off-by: akw27@cl.cam.ac.uk

tools/misc/xend
tools/python/xen/lowlevel/xu/xu.c
tools/xcs/xcs.c
tools/xcs/xcs_proto.h
tools/xcs/xcsdump.c

index 5c9757ec23203d42b7a2ed6333c3e855b711ba43..fe30a13a45fd24e61d3d005ba58d8639a6c9a57d 100644 (file)
@@ -24,7 +24,7 @@ import sys
 import socket
 import time
 
-XCS_PORT = 1633
+XCS_PATH = "/var/xen/xcs_socket"
 XCS_EXEC = "/usr/sbin/xcs"
 XCS_LOGFILE = "/var/log/xcs.log"
 
@@ -100,9 +100,9 @@ def xcs_running():
     """ See if the control switch is running.
     """
     ret = 1
-    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     try:
-        s.connect( ("127.0.0.1", XCS_PORT) )
+        s.connect( (XCS_PATH) )
     except:
         ret = 0
     s.close()
@@ -118,7 +118,7 @@ def main():
     
     if (not xcs_running()):
         if os.fork():
-            time.sleep(1) # let xcs start
+            time.usleep(500) # let xcs start
         else:
             try:
                 logfile = os.open(XCS_LOGFILE, 
index 12f27d63db26c1441ad0234c2481cd45c9296dd2..16768429b28cca5b4c6e370b02137438df14ae19 100644 (file)
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 #include <sys/mman.h>
 #include <sys/poll.h>
 #include <sys/sysmacros.h>
-#include <netinet/in.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
@@ -87,36 +87,34 @@ static int xcs_ctrl_read(xcs_msg_t *msg);
 static int xcs_data_send(xcs_msg_t *msg);
 static int xcs_data_read(xcs_msg_t *msg);
 
-static int xcs_connect(char *ip, short port)
+static int xcs_connect(char *path)
 {
-    struct sockaddr_in addr;
-    int ret, flags;
+    struct sockaddr_un addr;
+    int ret, len, flags;
     xcs_msg_t msg;
 
     if (xcs_data_fd != -1) /* already connected */
         return 0;
     
-    xcs_ctrl_fd = socket(AF_INET, SOCK_STREAM, 0);
+    xcs_ctrl_fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (xcs_ctrl_fd < 0)
     {
         printf("error creating xcs socket!\n");
         goto fail;
     }
     
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(port);
-    addr.sin_addr.s_addr = inet_addr(ip);
-    memset(&(addr.sin_zero), '\0', 8);
+    addr.sun_family = AF_UNIX;
+    strcpy(addr.sun_path, path);
+    len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
 
-    ret = connect(xcs_ctrl_fd, (struct sockaddr *)&addr, 
-            sizeof(struct sockaddr));
+    ret = connect(xcs_ctrl_fd, (struct sockaddr *)&addr, len);
     if (ret < 0) 
     {
         printf("error connecting to xcs(ctrl)! (%d)\n", errno);
         goto ctrl_fd_fail;
     }
 
-    //set_cloexec(xcs_ctrl_fd);
+    /*set_cloexec(xcs_ctrl_fd);*/
             
     msg.type = XCS_CONNECT_CTRL;
     msg.u.connect.session_id = xcs_session_id;
@@ -131,20 +129,18 @@ static int xcs_connect(char *ip, short port)
     xcs_session_id = msg.u.connect.session_id;
     
     /* now the data connection. */
-    xcs_data_fd = socket(AF_INET, SOCK_STREAM, 0);
+    xcs_data_fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (xcs_data_fd < 0)
     {
         printf("error creating xcs data socket!\n");
         goto ctrl_fd_fail;
     }
     
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(port);
-    addr.sin_addr.s_addr = inet_addr(ip);
-    memset(&(addr.sin_zero), '\0', 8);
+    addr.sun_family = AF_UNIX;
+    strcpy(addr.sun_path, path);
+    len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
     
-    ret = connect(xcs_data_fd, (struct sockaddr *)&addr, 
-            sizeof(struct sockaddr));
+    ret = connect(xcs_data_fd, (struct sockaddr *)&addr, len);
     if (ret < 0) 
     {
         printf("error connecting to xcs(data)! (%d)\n", errno);
@@ -447,7 +443,7 @@ static PyObject *xu_notifier_new(PyObject *self, PyObject *args)
     for (i = 0; i < XCS_RING_SIZE; i++) 
         REQ_RING_ENT(i) = RSP_RING_ENT(i) = NULL;
     
-    (void)xcs_connect("127.0.0.1", XCS_TCP_PORT);
+    (void)xcs_connect(XCS_SUN_PATH);
     
 
     return (PyObject *)xun;
index f0b1a96a8868b46a596c505c362425d4826cacca..f26e3c2f3949e1742eba9886b660a78b2fcb9e0b 100644 (file)
@@ -71,8 +71,7 @@
 #include <string.h>
 #include <signal.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <sys/un.h>
 #include <errno.h>
 #include <malloc.h>
 #include <fcntl.h>
@@ -89,27 +88,28 @@ static int dom_port_map_size = 0;
 
 static void map_dom_to_port(u32 dom, int port)
 {
-       if (dom >= dom_port_map_size) {
-               dom_port_map = (int *)realloc(dom_port_map,
-                                             (dom + 10) * sizeof(dom_port_map[0]));
-
-               if (dom_port_map == NULL) {
-                       perror("realloc(dom_port_map)");
-                       exit(1);
-               }
-
-               for (; dom_port_map_size < dom + 10; dom_port_map_size++) {
-                       dom_port_map[dom_port_map_size] = -1;
-               }
-       }
+    if (dom >= dom_port_map_size) {
+        dom_port_map = (int *)realloc(dom_port_map,
+                                      (dom + 256) * sizeof(dom_port_map[0]));
+
+        if (dom_port_map == NULL) {
+            perror("realloc(dom_port_map)");
+            exit(1);
+        }
+
+        for (; dom_port_map_size < dom + 10; dom_port_map_size++) {
+            dom_port_map[dom_port_map_size] = -1;
+        }
+    }
 
-       dom_port_map[dom] = port;
+    dom_port_map[dom] = port;
 }
 
-static int dom_to_port(u32 dom) {
-       if (dom >= dom_port_map_size) return -1;
+static int dom_to_port(u32 dom) 
+{
+    if (dom >= dom_port_map_size) return -1;
 
-       return dom_port_map[dom];
+    return dom_port_map[dom];
 }
 
 static void init_interfaces(void)
@@ -218,37 +218,34 @@ void put_interface(control_channel_t *cc)
 /* ------[ Simple helpers ]------------------------------------------------*/
 
 /* listen_socket() is straight from paul sheer's useful select_tut manpage. */
-static int listen_socket (int listen_port
+static int listen_socket (char *listen_path
 {
-    struct sockaddr_in a;
+    struct sockaddr_un a;
     int s;
     int yes;
 
-    if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) 
+    if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) 
     {
         perror ("socket");
         return -1;
     }
     
     yes = 1;
-    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
-        (char *) &yes, sizeof (yes)) < 0) 
-    {
-        perror ("setsockopt");
-        close (s);
-        return -1;
-    }
 
     memset (&a, 0, sizeof (a));
-    a.sin_port = htons (listen_port);
-    a.sin_family = AF_INET;
+    a.sun_family = AF_UNIX;
+    strcpy(a.sun_path, listen_path);
+
+    /* remove an old socket if it exists. */
+    unlink(listen_path);
+
     if (bind(s, (struct sockaddr *) &a, sizeof (a)) < 0) 
     {
         perror ("bind");
         close (s);
         return -1;
     }
-    printf ("accepting connections on port %d\n", (int) listen_port);
+    printf ("accepting connections on path %s\n", listen_path);
     listen (s, 10);
     return s;
 }
@@ -626,13 +623,13 @@ void gc_ufd_list( unbound_fd_t **ufd )
     }
 }
 
-int main (int argc, char*argv[])
+int main (int argc, char *argv[])
 {
     int listen_fd, evtchn_fd;
     unbound_fd_t *unbound_fd_list = NULL, **ufd;
     struct timeval timeout = { XCS_GC_INTERVAL, 0 };
     connection_t **con;
-    
+
     /* Initialize xc and event connections. */
     if (ctrl_chan_init() != 0)
     {
@@ -650,7 +647,7 @@ int main (int argc, char*argv[])
     init_interfaces();
     init_bindings();
     
-    listen_fd = listen_socket(XCS_TCP_PORT);
+    listen_fd = listen_socket(XCS_SUN_PATH);
    
     /* detach from our controlling tty so that a shell does hang waiting for
        stopped jobs. */
@@ -742,7 +739,7 @@ int main (int argc, char*argv[])
         /* CASE 2: New connection on the listen port. */
         if ( FD_ISSET ( listen_fd, &rd ))
         {
-            struct sockaddr_in remote_addr;
+            struct sockaddr_un remote_addr;
             int size;
             memset (&remote_addr, 0, sizeof (remote_addr));
             size = sizeof remote_addr;
index ea227c2ff7a55bcde08e05149a03b487906a5f4e..10ac7dafedd8a1cc83a3746310dcf8efadcc918f 100644 (file)
@@ -9,7 +9,7 @@
 #ifndef  __XCS_PROTO_H__
 #define  __XCS_PROTO_H__
 
-#define XCS_TCP_PORT     1633
+#define XCS_SUN_PATH     "/var/xen/xcs_socket"
 
 /* xcs message types: */
 #define XCS_CONNECT_CTRL       0 /* This is a control connection.     */
index dcfd2c9119e00deaf7bd50dc036f64e7a7a9a625..35ddcd4e3f73092d317cdc70eecc2fa00b885f21 100644 (file)
@@ -11,8 +11,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <sys/un.h>
 #include <ctype.h>
 #include <xc.h>
 #include <xen/xen.h>
 static int xcs_ctrl_fd = -1; /* connection to the xcs server. */
 static int xcs_data_fd = -1; /* connection to the xcs server. */
 
-int tcp_connect(char *ip, short port)
+int sock_connect(char *path)
 {
-    struct sockaddr_in addr;
-    int ret, fd;
+    struct sockaddr_un addr;
+    int ret, len, fd;
 
-    fd = socket(AF_INET, SOCK_STREAM, 0);
+    fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (fd < 0)
     {
         printf("error creating xcs socket!\n");
         return -1;
     }
 
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(port);
-    addr.sin_addr.s_addr = inet_addr(ip);
-    memset(&(addr.sin_zero), '\0', 8);
+    addr.sun_family = AF_UNIX;
+    strcpy(addr.sun_path, path);
+    len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
 
-    ret = connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
+    ret = connect(fd, (struct sockaddr *)&addr, len);
     if (ret < 0) 
     {
         printf("error connecting to xcs!\n");
@@ -50,7 +48,7 @@ int tcp_connect(char *ip, short port)
     return fd;
 }
 
-void tcp_disconnect(int *fd)
+void sock_disconnect(int *fd)
 {
     close(*fd);
     *fd = -1;
@@ -91,7 +89,7 @@ int main(int argc, char* argv[])
         if ((strlen(argv[1]) >=2) && (strncmp(argv[1], "-v", 2) == 0))
             verbose = 1;
     
-    ret = tcp_connect("127.0.0.1", XCS_TCP_PORT);
+    ret = sock_connect(XCS_SUN_PATH);
     if (ret < 0) 
     {
         printf("connect failed!\n"); 
@@ -109,7 +107,7 @@ int main(int argc, char* argv[])
         exit(-1);
     }
     
-    ret = tcp_connect("127.0.0.1", XCS_TCP_PORT);
+    ret = sock_connect(XCS_SUN_PATH);
     if (ret < 0) 
     {
         printf("connect failed!\n");